<style>
    .areabox{
        padding:2px;

    }
    .spectype{
        padding:6px;
        width:40px;
    }

    .weight_rule li{
        float:left;
        padding:4px;
        text-align:left;
    }
    .weight_left{
        width:210px;

    }
    .weight_right{
        width:300px;
    }
    .weight_rule ul{
        width:600px;
    }
</style>


        <div class="division">


            <table  border="0" cellpadding="0" cellspacing="0" class="girdlist">
                       <tr>
                         <th>绑定店铺:<{help}>不选择，默认为全部<{/help}></th>
                            <td>
                                <{foreach from=$shopData item=order}>
                                <label><input type="checkbox" name="item[]" value="<{$order.shop_id}>"><span><{$order.name}></span></label>

                                <{/foreach}>
                           </td>

                        </tr>
                    <tr>
                      <th>绑定物料:<{help}>不选择，默认为全部<{/help}></th>
                      <td>
                        <{button label="物料库频道" id="material-find-btn" }>&nbsp;&nbsp;<{button type="button" id="material-delall-btn" label="全部删除" }>
                        <input type="hidden" id="material_sales" name="material_sales_bn">
                        <table class="gridlist" id="material_table"  style="margin:4px 0;width:100%;">
                          <thead>
                          <tr>
                            <th>编码</th>
                            <th style="width:240px;">名称</th>
                            <th style="display: none">数量</th>
                            <th style="display: none">促销价格贡献占比(1-100整数数值)</th>
                            <th style="width:30px;">删除</th>
                          </tr>
                          </thead>
                          <tbody id="dataNode">
                          <tr>
                            <td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>
                          </tr>
                          </tbody>
                        </table>


                    </td>
                    </tr>
            </table>
        </div>
<script>

   /* function fun(){
        var k;
        var obj = document.getElementsByName("item");
        var check_val = [];
        for(  k in obj){
            if(obj[k].checked)
                check_val.push(obj[k].value);
        }
        alert(check_val);
    }*/
    (function() {
        window.addEvent('domready', function() {

            var callurl='index.php?app=material&ctl=admin_material_basic&act=getMaterial',store=[];

            var tpl='<tr key="{bm_id}" id="bm_{bm_id}" title="点击选取/反选此条记录">'
                +'  <td>{material_bn}</td><td class="material-name">{material_name}</td>'
                +'  <td style="display: none"><input type="text" value="{num}" key="num" vtype="number&amp;&amp;required" tname="at[_PRIMARY_]" size="6"></td>'
                +'  <td style="display: none"><input type="text" vtype="number&amp;&amp;required"  tname="pr[_PRIMARY_]" key="rate" value="{rate}" size="5"></td>'
                +'  <td><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'
                +'</tr>';

            $('material-find-btn').addEvent('click',function(e){
                var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=material&ctl=admin_material_basic&act=findMaterial');
                new finderDialog(url,{params:{url:callurl,name:'bm_id[]'},width:1000,height:660,
                    onCallback:function(rs){
                        if(!rs)return;
                        rs=JSON.decode(rs);
                        init(rs);
                    }
                });
            });

            var pag,rows;
            function emptyData(){

              $('material_sales').value = '';
                var noData='<tr>'
                    +'<td colspan="8" style="padding:0;"><div class="note" style="margin:0;"> 暂无物料信息 </div></td>'
                    +'</tr>';
                $('dataNode').set('html',noData);
            }

            function createProduct(data){
                var material_sales;
                var materialbn = '';
                for(var i=0;i<data.length;i++){
                    materialbn += data[i].material_bn + ",";
                }
              $('material_sales').value = materialbn;

                pag=new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':5,PRIMARY_ID:'bm_id',
                    'onShow':function(){
                        var _this=this;
                        $$('#dataNode input[type]').addEvent('change',function(e){
                            var pid=this.getParent('tr').get('key'),value=this.value;

                            _this.editData(pid,[this.get('key'),value]);
                        });

                        rows=$ES('#dataNode tr');
                        rows.each(function(item,i){
                            item.addEvent('click',function(e){
                                this.toggleClass('selected');
                            });
                            item.getElement('.btn-delete-item').addEvent('click',function(e){
                                if(_this.selectData(item.get('key')) && confirm('确定要删除 '+ _this.selectData(item.get('key'))['material_name'] +' 吗？')){
                                  _this.delData(item.get('key'));
                                  //删除material_sales中对应的值
                                  var materialbn_current = $('material_sales').value;
                                  var materialbn_del = item.getElements("td")[0].get('html');

                                  $('material_sales').value =  materialbn_current.substring(0,materialbn_current.lastIndexOf(materialbn_del))+
                                  materialbn_current.substring(materialbn_current.lastIndexOf(materialbn_del)+materialbn_del.length+1,materialbn_current.length);
                                }
                                if(!$E('#dataNode tr')) emptyData();
                            });
                            item.getElement('input[tname^=at]').addEvent('keypress',function(e){
                                if(e.code==13) $E('#pfba input').focus();
                            });
                        });

                        if(this.data.length) rows[0].getElement('input[key^=num]').focus();

                        $ES('.material-name').removeEvent('mouseover').addEvent('mouseover',function(e){
                            if (this.get('visibility')=='false')
                            {
                                var e  = new Event(e), el = e.target;
                                visiTips.attach(el);
                                el.addEvent('mouseleave',function(){
                                    this.removeClass('active');
                                });
                                el.fireEvent('mouseenter',e);
                            }
                        });
                    }
                });
            }

            function init(rs){
                var tmparr=findProduct(rs,'bm_id');
                store.unshift.apply(store,tmparr.reverse());
                createProduct(store);
            }

            function findProduct(arr,PRIMARY){


                if(!store.length)return arr;
                store.each(function(a){
                    arr.each(function(b){
                        if(a[PRIMARY]==b[PRIMARY])arr.erase(b);
                    });
                });
                return arr;
            }

            function delProduct(obj,arr){
                arr.each(function(d){obj.delData(d);});
            }

            $('material-delall-btn').addEvent('click',function(e){
                if(!pag||!pag.data)return;
                var delarr=[];
                pag.data.each(function(d){
                    delarr.push(d['bm_id']);
                });
                if(confirm('确认删除全部物料吗？')){
                    delProduct(pag,delarr);
                    emptyData();
                }
            });


        });
    })();

    function material_object_callback(rs,handle){
        var p_node = handle.getParent('div');
        var html = '已选择了1个物料,'+"<a href='javascript:void(0);' onclick='material_selected_show();'>查看关联的物料.</a>";
        if ($defined($('hand-selected-product')))
        {
            $('hand-selected-product').setHTML(html);
        } else {
            var div = new Element('div',{'html':html,'id':'hand-selected-product'});
            div.injectAfter(p_node);
        }
    }

    function material_selected_show(){
        new Dialog('index.php?app=material&ctl=admin_material_sales&act=showMaterials',{
            ajaxoptions:{data:$('hand-selected-product').getPrevious('div'),method:'post'}
        });
    }

</script>